package net.xdclass.service.impl;

import lombok.extern.slf4j.Slf4j;
import net.xdclass.controller.request.LinkGroupAddRequest;
import net.xdclass.controller.request.LinkGroupUpdateRequest;
import net.xdclass.interceptor.LoginInterceptor;
import net.xdclass.manager.LinkGroupManager;
import net.xdclass.model.LinkGroupDO;
import net.xdclass.service.LinkGroupService;
import net.xdclass.vo.LinkGroupVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class LinkGroupServiceImpl implements LinkGroupService {

    @Autowired
    private LinkGroupManager linkGroupManager;

    @Override
    public int add(LinkGroupAddRequest linkGroupAddRequest) {
        // 获取用户的登录信息
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        LinkGroupDO linkGroupDO = new LinkGroupDO();
        linkGroupDO.setTitle(linkGroupAddRequest.getTitle());
        linkGroupDO.setAccountNo(accountNo);
        int rows = linkGroupManager.add(linkGroupDO);

        return rows;
    }

    @Override
    public int del(long groupId) {
        // 获取用户的登录信息
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();

        return linkGroupManager.del(groupId, accountNo);
    }

    @Override
    public LinkGroupVO detail(long groupId) {
        // 获取用户的登录信息
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupVO linkGroupVO = new LinkGroupVO();
        LinkGroupDO linkGroupDO = linkGroupManager.detail(groupId, accountNo);
        BeanUtils.copyProperties(linkGroupDO, linkGroupVO);
        return linkGroupVO;
    }

    @Override
    public List<LinkGroupVO> listAllGroup() {
        // 获取用户的登录信息
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        List<LinkGroupVO> groupVOList = linkGroupManager.listAllGroup(accountNo).stream().map(linkGroupDO -> {
            LinkGroupVO linkGroupVO = new LinkGroupVO();
            BeanUtils.copyProperties(linkGroupDO, linkGroupVO);
            return linkGroupVO;
        }).collect(Collectors.toList());
        return groupVOList;
    }

    @Override
    public int updateById(LinkGroupUpdateRequest request) {
        // 获取用户的登录信息
        Long accountNo = LoginInterceptor.threadLocal.get().getAccountNo();
        LinkGroupDO linkGroupDO = new LinkGroupDO();
        BeanUtils.copyProperties(request,linkGroupDO);
        linkGroupDO.setAccountNo(accountNo);
        return linkGroupManager.updateById(linkGroupDO);
    }


}
